package com.app.couponsharelife.ui.main.home.search

import android.graphics.drawable.AnimationDrawable
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.HomeChiocenGoodsBean
import com.app.couponsharelife.data.bean.RemoteResultBean
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.adapter.GoodsSearchAdapter
import com.app.couponsharelife.ui.listener.MyMultiPurposeListener
import com.app.couponsharelife.utils.AppUtils
import com.app.couponsharelife.utils.DataUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_hqt.*
import kotlinx.android.synthetic.main.base_refresh_head.*
import kotlinx.android.synthetic.main.base_title_white.*

/**
 *Created by xct on 2019/1/19
 *Description:
 */
class HQTGoodsListActivity:BaseActivity() {
    private var pageIndex=1
    private var goodsList= arrayListOf<TaobaoGoodBean>()
    private var adapter: GoodsSearchAdapter?=null
    private var favoritesID=""
    private var name=""
    private var imagUrl=""
    override val viewLayoutId: Int
        get() = R.layout.activity_hqt

    override fun initViews() {
        AppUtils.initImmersionBar(this,true)
        setRefresh()
        favoritesID=intent.getStringExtra("FavoritesID")
        name=intent.getStringExtra("Name")?:""
        imagUrl=intent.getStringExtra("ImagUrl")?:""
        basetitle_title.text=name
        adapter=GoodsSearchAdapter(this,goodsList,"TB")
        jdgoodslist_rv.layoutManager= GridLayoutManager(this,2)
        jdgoodslist_rv.adapter=adapter
        getGoodsList(false)
        basetitle_llclose.setOnClickListener { finish() }
        Glide.with(this).asBitmap()
                .apply(RequestOptions().error(R.drawable.ic_app_place_holder2))
                .load(Contacts.NEWREMOTE_IMAGE_URL +imagUrl)
                .into(jdgoodslist_ivBanner)
    }
    private fun setRefresh(){
        hotsale_refreshimg.setImageResource(R.drawable.anim_refresh)
        val animationDrawable=hotsale_refreshimg.drawable as AnimationDrawable
        jdgoodslist_smart.setOnMultiPurposeListener(object : MyMultiPurposeListener {
            override fun headerReleased() {
                animationDrawable.start()
            }

            override fun headerMoving(percent: Float) {
                setAnima(percent)
            }

            override fun headerFinish() {
                hotsale_refreshimg.setImageResource(R.drawable.refresh_0)
                animationDrawable.stop()
            }

            override fun loadMore() {
                getGoodsList(false)
            }

            override fun refresh() {
                pageIndex=1
                getGoodsList(true)
            }

        })
    }
    private fun setAnima(vlaue:Float){
        val layoutParams=hotsale_refreshimg.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.matchConstraintPercentHeight=vlaue/2
        layoutParams.dimensionRatio="0.875:1"
        hotsale_refreshimg.layoutParams=layoutParams
    }


    private fun getGoodsList(isRefresh:Boolean){
        if (!isRefresh&&pageIndex==1){
            showProgressDialog("加载中...")
        }
        val map=HashMap<String,String>()
        map["pageNo"]=pageIndex.toString()
        map["pageSize"]="10"
        map["userId"]=AppUtils.getString(Contacts.USERID,"")
        map["favoritesId"]=favoritesID
        RetrofitWorker.retrofitWorker.getFavoritesItemPage(map)
                .subscribeOn(Schedulers.io())
                .map { DataUtils().taobaoData(it) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<HomeChiocenGoodsBean>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<HomeChiocenGoodsBean>) {
                        if (!isRefresh&&pageIndex==1){
                            dismissProgressDialog()
                        }
                        if (it.data!=null&&it.data!!.recordss.isNotEmpty()){
                            if (pageIndex==1){
                                goodsList.clear()
                                if (isRefresh){
                                    jdgoodslist_llerror.visibility= View.GONE
                                    jdgoodslist_rv.visibility= View.VISIBLE
                                    jdgoodslist_smart.finishRefresh()
                                }
                            }else{
                                jdgoodslist_smart.finishLoadMore()
                            }
                            goodsList.addAll(it.data!!.recordss)
                            if (it.data!!.recordss.size<10){
                                jdgoodslist_smart.finishLoadMoreWithNoMoreData()
                            }
                            pageIndex++
                            adapter!!.notifyDataSetChanged()
                        }else{
                            if (pageIndex==1&&goodsList.size==0){
                                jdgoodslist_smart.finishLoadMoreWithNoMoreData()
                                jdgoodslist_llerror.visibility= View.VISIBLE
                                jdgoodslist_rv.visibility= View.GONE
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        if (!isRefresh&&pageIndex==1){
                            dismissProgressDialog()
                        }
                        if (isRefresh){
                            jdgoodslist_smart.finishRefresh()
                        }
                        if (pageIndex>1){
                            jdgoodslist_smart.finishLoadMore()
                        }
                        if (pageIndex==1){
                            jdgoodslist_smart.finishLoadMoreWithNoMoreData()
                            jdgoodslist_llerror.visibility= View.VISIBLE
                            jdgoodslist_rv.visibility= View.GONE
                        }
                        if (e is OtherException){
                            //showToastMsg(e.msg)
                        }else{
                            //Log.e("eee","---->"+e.toString())
                            showToastMsg("网络加载出错了！")
                        }
                    }

                })
    }

    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}